home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / AvantBrowser / asetup.exe / _data / webkit / chrome_100_percent.pak / Unnamed File 000016.txt < prev    next >
Text File  |  2013-04-03  |  4KB  |  122 lines

  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4.  
  5. var chromeHidden = requireNative('chrome_hidden').GetChromeHidden();
  6. var lastError = require('lastError');
  7. var natives = requireNative('sendRequest');
  8. var validate = require('schemaUtils').validate;
  9.  
  10. // Callback handling.
  11. var requests = [];
  12. chromeHidden.handleResponse = function(requestId, name,
  13.                                        success, responseList, error) {
  14.   try {
  15.     var request = requests[requestId];
  16.     if (success) {
  17.       lastError.clear();
  18.     } else {
  19.       if (!error) {
  20.         error = "Unknown error.";
  21.       }
  22.       console.error("Error during " + name + ": " + error);
  23.       lastError.set(error);
  24.     }
  25.  
  26.     if (request.customCallback) {
  27.       var customCallbackArgs = [name, request].concat(responseList);
  28.       request.customCallback.apply(request, customCallbackArgs);
  29.     }
  30.  
  31.     if (request.callback) {
  32.       // Validate callback in debug only -- and only when the
  33.       // caller has provided a callback. Implementations of api
  34.       // calls my not return data if they observe the caller
  35.       // has not provided a callback.
  36.       if (chromeHidden.validateCallbacks && !error) {
  37.         try {
  38.           if (!request.callbackSchema.parameters) {
  39.             throw new Error("No callback schemas defined");
  40.           }
  41.  
  42.           validate(responseList, request.callbackSchema.parameters);
  43.         } catch (exception) {
  44.           return "Callback validation error during " + name + " -- " +
  45.                  exception.stack;
  46.         }
  47.       }
  48.  
  49.       request.callback.apply(request, responseList);
  50.     }
  51.   } finally {
  52.     delete requests[requestId];
  53.     lastError.clear();
  54.   }
  55.  
  56.   return undefined;
  57. };
  58.  
  59. function prepareRequest(args, argSchemas) {
  60.   var request = {};
  61.   var argCount = args.length;
  62.  
  63.   // Look for callback param.
  64.   if (argSchemas.length > 0 &&
  65.       argSchemas[argSchemas.length - 1].type == "function") {
  66.     request.callback = args[args.length - 1];
  67.     request.callbackSchema = argSchemas[argSchemas.length - 1];
  68.     --argCount;
  69.   }
  70.  
  71.   request.args = [];
  72.   for (var k = 0; k < argCount; k++) {
  73.     request.args[k] = args[k];
  74.   }
  75.  
  76.   return request;
  77. }
  78.  
  79. // Send an API request and optionally register a callback.
  80. // |optArgs| is an object with optional parameters as follows:
  81. // - noStringify: true if we should not stringify the request arguments.
  82. // - customCallback: a callback that should be called instead of the standard
  83. //   callback.
  84. // - nativeFunction: the v8 native function to handle the request, or
  85. //   StartRequest if missing.
  86. // - forIOThread: true if this function should be handled on the browser IO
  87. //   thread.
  88. // - preserveNullInObjects: true if it is safe for null to be in objects.
  89. function sendRequest(functionName, args, argSchemas, optArgs) {
  90.   if (!optArgs)
  91.     optArgs = {};
  92.   var request = prepareRequest(args, argSchemas);
  93.   if (optArgs.customCallback) {
  94.     request.customCallback = optArgs.customCallback;
  95.   }
  96.   // JSON.stringify doesn't support a root object which is undefined.
  97.   if (request.args === undefined)
  98.     request.args = null;
  99.  
  100.   // TODO(asargent) - convert all optional native functions to accept raw
  101.   // v8 values instead of expecting JSON strings.
  102.   var doStringify = false;
  103.   if (optArgs.nativeFunction && !optArgs.noStringify)
  104.     doStringify = true;
  105.   var requestArgs = doStringify ?
  106.       chromeHidden.JSON.stringify(request.args) : request.args;
  107.   var nativeFunction = optArgs.nativeFunction || natives.StartRequest;
  108.  
  109.   var requestId = natives.GetNextRequestId();
  110.   request.id = requestId;
  111.   requests[requestId] = request;
  112.   var hasCallback = request.callback || optArgs.customCallback;
  113.   return nativeFunction(functionName,
  114.                         requestArgs,
  115.                         requestId,
  116.                         hasCallback,
  117.                         optArgs.forIOThread,
  118.                         optArgs.preserveNullInObjects);
  119. }
  120.  
  121. exports.sendRequest = sendRequest;
  122.